#include "uart.h"
int set_opt(int fd, int nSpeed, int nBits, char nEvent, int nStop)
{
    struct termios newtio, oldtio;

    if ( tcgetattr( fd, &oldtio) != 0)
    {
        perror("SetupSerial .\n");
        return -1;
    }

    bzero( &newtio, sizeof( newtio ) );
    newtio.c_cflag |= CLOCAL | CREAD;
    newtio.c_cflag &= ~CSIZE;

    switch ( nBits )
    {
    case 7:
        newtio.c_cflag |= CS7;
        break;
    case 8:
        newtio.c_cflag |= CS8;
        break;
    }

    switch ( nEvent )
    {
    case 'O':
        newtio.c_cflag |= PARENB;
        newtio.c_cflag |= PARODD;
        newtio.c_iflag |= (INPCK | ISTRIP);
        break;
    case 'E':
        newtio.c_iflag |= (INPCK | ISTRIP);
        newtio.c_cflag |= PARENB;
        newtio.c_cflag &= ~PARODD;
        break;
    case 'N':
        newtio.c_cflag &= ~PARENB;
        break;
    }

    switch ( nSpeed )
    {
    case 2400:
        cfsetispeed(&newtio, B2400);
        cfsetospeed(&newtio, B2400);
        break;
    case 4800:
        cfsetispeed(&newtio, B4800);
        cfsetospeed(&newtio, B4800);
        break;
    case 9600:
        cfsetispeed(&newtio, B9600);
        cfsetospeed(&newtio, B9600);
        break;
    case 115200:
        cfsetispeed(&newtio, B115200);
        cfsetospeed(&newtio, B115200);
        break;
    case 460800:
        cfsetispeed(&newtio, B460800);
        cfsetospeed(&newtio, B460800);
        break;
    case 576000:
        cfsetispeed(&newtio, B576000);
        cfsetospeed(&newtio, B576000);
        break;
    case 921600:
        cfsetispeed(&newtio, B921600);
        cfsetospeed(&newtio, B921600);
        break;
    case 3000000:
        cfsetispeed(&newtio, B3000000);
        cfsetospeed(&newtio, B3000000);
        break;
    default:
        cfsetispeed(&newtio, B9600);
        cfsetospeed(&newtio, B9600);
        break;
    }

    if ( nStop == 1 )
        newtio.c_cflag &= ~CSTOPB;
    else if ( nStop == 2 )

        newtio.c_cflag |= CSTOPB;
    newtio.c_cc[VTIME] = 0;
    newtio.c_cc[VMIN] = 0;

    tcflush(fd, TCIOFLUSH);

    if ((tcsetattr(fd, TCSANOW, &newtio)) != 0)
    {
        printf("com set error\n");
        return -1;
    }

    return 0;
}


int uart_init(char *ComPort, unsigned char *Setting,int BandRate)
{
	int fd,ret;
	fd =  open( ComPort, O_RDWR | O_NOCTTY | O_NONBLOCK);
    if(fd < 0) {
        printf("open %s faile ,ret = %d\n",ComPort,fd);
        return -1;
    }
	/*
	 if (fcntl(fd, F_SETFL, 0) < 0)
        printf("fcntl failed!\n");
    else
        printf("fcntl=%d\n", fcntl(fd, F_SETFL, 0));
    */
    if (isatty(STDIN_FILENO) == 0)    
        printf("standard input is not a terminal device\n");
    ret = set_opt(fd,BandRate, 8,'N', 1);
    if(ret < 0) {
         printf("set uart  fail ! \n");
        return -1;
    }
    //printf("%s init ok,ret = %d\n",ComPort,fd);
    return fd;
}


int InitSerial(char *COM, unsigned int  Baudrate)
{
	int retry = 0;
	char m_Log[512] = {0};
	int m_Ret = -1;
	sprintf(m_Log,"Func:%s COM:%s Baudrate:%d ","InitSerial",COM,Baudrate);
	printf("%s \n",m_Log);
openretry:
	m_Ret = uart_init(COM, NULL,Baudrate);
	memset(m_Log,0,sizeof(m_Log));
	sprintf(m_Log,"Func:%s---->%s,retry %d ","InitSerial",((m_Ret > 0) ? "SUCCESS" : "ERROR"),retry);
	printf("%s \n",m_Log);
	if(m_Ret < 0)
	{
			if(retry < 5)
			{
				retry++;
				usleep(500*1000);
				goto openretry;
			}	
	}
	return m_Ret;
}



